//
//  HomeController.m
//  carowner
//
//  Created by 孙可 on 2019/5/13.
//  Copyright © 2019年 ggj. All rights reserved.
//

#import "HomeController.h"
#import "ItemMoreView.h"
#import "BannerCell.h"
#import "BannerEntity.h"
#import "HomeMenuCell.h"
#import "ServiceShopCell.h"
#import "HotServiceCell.h"
#import "ShopChooseController.h"

@interface HomeController ()


@property(nonatomic,strong)UIScrollView * scrollerView;
@property(nonatomic,strong) TYCyclePagerView * pagerView;
@property(nonatomic,strong) UIView * topView;
@property(nonatomic,strong) UIButton * tvCity;
@property(nonatomic,strong) UILabel * tvWeather;
@property(nonatomic,strong) UILabel * todayOil;
@property(nonatomic,strong) UIButton * tipBtn;
@property(nonatomic,strong) UILabel * tipNum;

@property(nonatomic,strong) UICollectionView * menuView;
@property(nonatomic,strong) UUMarqueeView * singleMarView;

@property(nonatomic,strong) ItemMoreView * serviceMoreView;
@property(nonatomic,strong) ItemMoreView * actionMoreView;
@property(nonatomic,strong) UITableView * serviceTableView;
@property(nonatomic,strong) UITableView * actionTableView;

@property(nonatomic,strong)NSArray * bannerArray;
@property(nonatomic,strong)NSArray * singleMarData;
@property(nonatomic,strong)NSArray * menuImageArray;
@property(nonatomic,strong)NSArray * menuTitleArray;


@property(nonatomic,strong)AMapLocationManager * locationManager;
/**
 位置管理器
 */
@property (nonatomic, strong)CLLocationManager *manager;

@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _menuImageArray = [[NSArray alloc] initWithObjects:@"smwechart/xcxIcon/icon-xiche.png",@"smwechart/xcxIcon/icon-baoyang.png",@"smwechart/xcxIcon/icon-meirong.png",@"smwechart/xcxIcon/icon-wuyoucha.png",@"smwechart/xcxIcon/icon-lingjianfangwei.png",@"smwechart/xcxIcon/icon-daolujiuyuan.png",@"smwechart/xcxIcon/icon-cheliangbaike.png" ,@"smwechart/xcxIcon/icon-gengduo.png",nil];
    
    _menuTitleArray = [[NSArray alloc] initWithObjects:@"我要洗车",@"我要保养",@"我要美容",@"无忧查",@"零件查询",@"道路救援",@"车辆百科",@"更多", nil];
    
    
    _scrollerView = [[UIScrollView alloc] init];
    _scrollerView.scrollEnabled = YES;
    _scrollerView.bounces = YES;
    [self.view addSubview:_scrollerView];
    
    [_scrollerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.width.mas_equalTo(kScreen_Width);
        make.top.equalTo(self.view).offset(kSafeArea_Top);
    }];
    
    _topView  = [[UIView alloc] init];
    _topView.backgroundColor = translucentBg;
    
    _tvCity = [UIButton buttonWithType:UIButtonTypeCustom];
    [_tvCity setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_tvCity setTitle:@"定位中..." forState:UIControlStateNormal];
    [_tvCity setFont:textFont];
    
    _tvWeather = [[UILabel alloc] init];
    _tvWeather.textColor = [UIColor whiteColor];
    _tvWeather.font = textFont;
    _tvWeather.text = @"获取中...";
    
    
    _manager = [CLLocationManager new];
    [_manager requestWhenInUseAuthorization];
    
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.locatingWithReGeocode = YES;
    self.locationManager.delegate = self;
    
    [self.locationManager startUpdatingLocation];
    
    
    // Do any additional setup after loading the view.
    _pagerView = [[TYCyclePagerView alloc] init];
    _pagerView.layer.borderWidth = 0;
    _pagerView.isInfiniteLoop = YES;
    _pagerView.autoScrollInterval = 3.0;
    _pagerView.dataSource = self;
    _pagerView.delegate = self;
    // registerClass or registerNib
    [_pagerView registerClass:[BannerCell class] forCellWithReuseIdentifier:@"cellId"];
    _todayOil = [[UILabel alloc] init];
    _todayOil.text = @"今日油价";
    _todayOil.font = textFont;
    
    _singleMarView = [[UUMarqueeView alloc] initWithFrame:CGRectMake(20.0f, 200, kScreen_Width - 40.0f, 40.0f)];
    _singleMarView.delegate = self;
    _singleMarView.timeIntervalPerScroll = 2.0f;
    _singleMarView.timeDurationPerScroll = 1.0f;
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    _menuView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 160) collectionViewLayout:flowLayout];
    _menuView.delegate = self;
    _menuView.dataSource = self;
    _menuView.contentInset = UIEdgeInsetsMake(10,0,10,0);
    _menuView.backgroundColor = [UIColor clearColor];
    [_menuView registerClass:[HomeMenuCell class] forCellWithReuseIdentifier:@"menuID"];
    _menuView.scrollEnabled = NO;

    
    UIView * lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = line_color;
    
    
    _serviceMoreView = [[ItemMoreView alloc] init];
    _serviceMoreView.tvTip.text = @"服务商";
    
    _serviceTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _serviceTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_serviceTableView registerClass:[ServiceShopCell class] forCellReuseIdentifier:@"shopID"];
    _serviceTableView.delegate = self;
    _serviceTableView.dataSource = self;
    _serviceTableView.scrollEnabled = NO;
    
    _actionMoreView = [[ItemMoreView alloc] init];
    _actionMoreView.tvTip.text = @"热销服务";
    
    _actionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _actionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_actionTableView registerClass:[HotServiceCell class] forCellReuseIdentifier:@"serviceID"];
    _actionTableView.delegate = self;
    _actionTableView.dataSource = self;
    _actionTableView.scrollEnabled = NO;
    
    [self.scrollerView addSubview:_pagerView];
    [self.scrollerView addSubview:_topView];
    [_topView addSubview:_tvCity];
    [_topView addSubview:_tvWeather];
    [self.scrollerView addSubview:_todayOil];
    [self.scrollerView addSubview:_singleMarView];
    [self.scrollerView addSubview:_menuView];
    [self.scrollerView addSubview:lineView1];
    [self.scrollerView addSubview:_serviceMoreView];
    [self.scrollerView addSubview:_serviceTableView];
    [self.scrollerView addSubview:_actionMoreView];
    [self.scrollerView addSubview:_actionTableView];
    
    [_singleMarView reloadData];

    
    [_pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.scrollerView);
        make.top.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 200));
    }];
    
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.scrollerView);
        make.top.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 45));
    }];
    
    [_tvCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_topView).offset(15);
        make.centerY.equalTo(_topView);
        make.size.mas_equalTo(CGSizeMake([NSString getTextWidth:@"定位中..." andFont:textFont],30));
    }];
    
    [_tvWeather mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_topView).offset(-15);
        make.centerY.equalTo(_topView);
        make.size.mas_equalTo(CGSizeMake([NSString getTextWidth:@"获取中..." andFont:textFont], 30));
    }];
    
    [_todayOil mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_pagerView.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(15);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    [_singleMarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_todayOil);
        make.left.equalTo(_todayOil.mas_right).offset(10);
        make.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 40));
    }];
    
    [_menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_singleMarView.mas_bottom);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 160));
    }];
    
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_menuView.mas_bottom).offset(10);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 1));
    }];
    
    [_serviceMoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView1.mas_bottom);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 40));

    }];
    
    [_serviceTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_serviceMoreView.mas_bottom);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 200));
    }];
    
    [_actionMoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_serviceTableView.mas_bottom);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 40));
    }];
    
    [_actionTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_actionMoreView.mas_bottom);
        make.left.right.equalTo(self.scrollerView);
        make.size.mas_equalTo(CGSizeMake(kScreen_Width, 380));
    }];
    
    [self.view layoutIfNeeded];
    
    
    NSDictionary * dt = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"type",@"1",@"position", nil];
    
    [[ApiManager sharedManager]getSlide:dt andBlock:^(id data, NSError *error) {
        id resultData = [data valueForKeyPath:ResultList];
        _bannerArray = [BannerEntity mj_objectArrayWithKeyValuesArray:resultData];
        [_pagerView reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView{
    return _bannerArray.count;
}


- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index{
    BannerCell *cell = [_pagerView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndex:index];
    BannerEntity * entity = _bannerArray[index];
    [MyUtils loadImageWithUrl:entity.path intoView:cell.bannerImage];
    return cell;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_singleMarView start];
    self.scrollerView.contentSize =  CGSizeMake(kScreen_Width,_actionTableView.frame.origin.y+380);
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_singleMarView pause];
}

-(TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame), CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 5;
    layout.itemHorizontalCenter = YES;
    return layout;
}

-(NSUInteger)numberOfVisibleItemsForMarqueeView:(UUMarqueeView *)marqueeView{
    return 1;
}

-(NSUInteger)numberOfDataForMarqueeView:(UUMarqueeView *)marqueeView{
    return 2;
//     return _singleMarData ? _singleMarData.count : 0;
}

- (void)createItemView:(UIView *)itemView forMarqueeView:(UUMarqueeView *)marqueeView{
    UILabel *content = [[UILabel alloc] initWithFrame:itemView.bounds];
    content.textColor = price_color;
    content.tag = 1001;
    content.font = [UIFont systemFontOfSize:12];
    [itemView addSubview:content];
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(itemView);
    }];
}

-(void)updateItemView:(UIView *)itemView atIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView *)marqueeView{
    UILabel * content = [itemView viewWithTag:1001];
    content.text = @"你好";
}

-(CGFloat)itemViewWidthAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView *)marqueeView{
    return 200;
}

-(CGFloat)itemViewHeightAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView *)marqueeView{
    return 40;
}


-(void)didTouchItemViewAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView *)marqueeView{
    
}

//接收位置更新,实现AMapLocationManagerDelegate代理的amapLocationManager:didUpdateLocation方法，处理位置更新
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    if (reGeocode)
    {
        NSLog(@"reGeocode:%@", reGeocode);
    }
}

- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager *)locationManager{
    NSLog(@"location:%@%@",manager,locationManager);
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeMenuCell * cell = [_menuView dequeueReusableCellWithReuseIdentifier:@"menuID" forIndexPath:indexPath];
    [MyUtils loadImageWithUrl:_menuImageArray[indexPath.row] intoView:cell.menuView];
    cell.menuLabel.text = _menuTitleArray[indexPath.row];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kScreen_Width/4, 80);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == 0){
        ShopChooseController * shopVc = [[ShopChooseController alloc] init];
        shopVc.selectIndex = 0;
        shopVc.menuViewStyle = WMMenuViewStyleLine;
        shopVc.automaticallyCalculatesItemWidths = YES;
        shopVc.progressViewIsNaughty = YES;
        shopVc.progressWidth = 10;
        shopVc.progressColor = themeColor;
        shopVc.titleColorNormal = title_color;
        shopVc.titleColorSelected = themeColor;
        [self.navigationController pushViewController:shopVc animated:YES];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView == _serviceTableView){
        return 2;
    }else if(tableView == _actionTableView){
        return 2;
    }else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == _serviceTableView){
        return 100;
    }else if(tableView == _actionTableView){
        return 190;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == _serviceTableView){
        ServiceShopCell * cell = [_serviceTableView dequeueReusableCellWithIdentifier:@"shopID"];
        return cell;
    }else if(tableView == _actionTableView){
        HotServiceCell * cell = [_actionTableView dequeueReusableCellWithIdentifier:@"serviceID"];
        return cell;
    }else{
        return nil;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
